﻿using System.Linq;

namespace Solution.BusinessObject
{
    public class ContentBO
    {
        /// <summary>
        /// Gets a collection of content that related to a specific content id and sort descending.
        /// </summary>
        /// <param name="category">Category of content.</param>
        /// <param name="content"> </param>
        /// <returns>A collection of <see cref="Content"/> was sort descending.</returns>
        public Content[] GetOldRelatedContents(Category category,
                                               Content content)
        {
            if (category == null || content == null)
            {
                return CommonBO.MasterEDM.Contents
                    .OrderByDescending(t => t.DisplayOrder).ToArray();
            }
            return CommonBO.MasterEDM.Contents
                .Where(t => t.Category.Id == category.Id
                            && t.Id < content.Id)
                .OrderByDescending(t => t.DisplayOrder)
                .ToArray();
        }

        /// <summary>
        /// Gets a <see cref="Content"/> by specific ID.
        /// </summary>
        public Content GetContent(Category category, int contentId)
        {
            return CommonBO.MasterEDM.Contents
                .FirstOrDefault(t => t.Category.Id == category.Id &&
                                     t.Id == contentId);
        }

        /// <summary>
        /// Gets lastest <see cref="Content"/> bases on Display order.
        /// </summary>
        public Content GetLastContent(Category category)
        {
            var max = CommonBO.MasterEDM.Contents
                .Where(t => t.DisplayOrder != null &&
                            t.Category.Id == category.Id)
                .Max(t => t.DisplayOrder);

            return CommonBO.MasterEDM.Contents
                .FirstOrDefault(t => t.DisplayOrder == max &&
                                     t.Category.Id == category.Id);
        }
    }
}
